package ntd.util.config.common.controller;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import ntd.util.config.common.exception.ComException;
import ntd.util.config.common.bo.ResponseBO;
import ntd.util.util.UtilPage;
import ntd.util.util.metadata.MetaDataBO;
import ntd.util.util.metadata.UtilMetaData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

public class BaseMybatisController<T, E extends IService<T>> extends ControllerEmptyMethods<T> {

    @Autowired
    private E iService;
    public E getService() {
        return iService;
    }

    @GetMapping
    public Object get(@ApiIgnore @RequestParam Map<String, String> params){
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        buildRule(queryWrapper, params.get("__rules"));
        beforeListQuery(queryWrapper);
        queryWrapper.orderByDesc("created_date");
        final IPage<T> page = iService.page(UtilPage.defaultPage(), queryWrapper);
        buildView(page);
        return page;
    }

    @GetMapping("list")
    @ApiOperation("标准-查询功能-不分页")
    public Object list(@ApiIgnore @RequestParam Map<String, String> params){
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        buildRule(queryWrapper, params.get("__rules"));
        beforeListQuery(queryWrapper);
        queryWrapper.orderByDesc("created_date");
        List<T> list = iService.list(queryWrapper);
        buildView(list);
        return list;
    }

    @GetMapping({"/{id}"})
    @ApiOperation("标准-详情功能")
    public T get(@PathVariable String id){
        return iService.getById(id);
    }

    @Transactional
    @PutMapping
    @ApiOperation("标准-添加功能")
    public T add(@Validated @RequestBody T obj) {
        final boolean b = this.iService.saveOrUpdate(beforeAdd(obj));
        if(!b){
            throw new ComException("添加异常！");
        }
        afterAdd(obj);
        return obj;
    }

    @Transactional
    @PostMapping
    @ApiOperation("标准-编辑功能")
    public T edit(@RequestBody T obj) {
        beforeEdit(obj);
        this.iService.saveOrUpdate(obj);
        afterEdit(obj);
        return obj;
    }

    @DeleteMapping({"/{id}"})
    @ApiOperation("标准-删除功能")
    public void remove(@PathVariable String id) {
        final T obj = iService.getById(id);
        if(!this.iService.removeById(id)){
            throw new ComException("删除失败，请稍后重试！");
        }
        afterRemove(obj);
    }

    @PostMapping(value = "/remove-all")
    @ApiOperation("标准-批量删除功能")
    public void removeAll(@RequestParam String ids) {
        iService.removeByIds(ListUtil.of(ids.split(",")));
    }

    /**
     * 获取对象元数据
     * @return
     */
    @PostMapping(value = "/get-attributes")
    @ApiOperation("标准-获取元数据信息")
    public ResponseBO<MetaDataBO> getAttributes(){
        return new ResponseBO(UtilMetaData.getAttributes(this));
    }

}
